import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'index.dart';

class HttpIpSetPage extends GetView<HttpIpSetController> {
  const HttpIpSetPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<HttpIpSetController>(
      init: HttpIpSetController(),
      id: "http_ip_set",
      builder: (_) {
        return Scaffold(
          appBar: AppBar(title: const Text("设置ip")),
          body: SafeArea(
            child: Padding(
              padding: const EdgeInsets.all(20),
              child: _buildForm(),
            ),
          ),
        );
      },
    );
  }

  Widget _buildForm() {
    return Form(
      key: controller.fromKey,
      autovalidateMode: AutovalidateMode.onUserInteraction,
      child: Column(
        children: [
          _buildTextField(controller.ipController, 'ip:', validateIp),
          const SizedBox(height: 20),
          _buildTextField(controller.portController, '端口号:', validatePort),
          const SizedBox(height: 20),
          _buildProtocolSelection(),
          const SizedBox(height: 40),
          ElevatedButton(
            child: const Text('确定'),
            onPressed: () {
              if (controller.fromKey.currentState!.validate()) {
                controller.onSave();
              }
            },
          ),
        ],
      ),
    );
  }

  String? validateIp(String? value) {
    if (value == null || value.isEmpty) {
      return '请输入IP地址';
    }
    // 可以添加更复杂的IP地址验证逻辑
    return null;
  }

  String? validatePort(String? value) {
    if (value == null || value.isEmpty) {
      return '请输入端口号';
    }
    if (int.tryParse(value) == null) {
      return '端口号必须是数字';
    }
    int port = int.parse(value);
    if (port < 1 || port > 65535) {
      return '端口号范围应在1-65535之间';
    }
    return null;
  }

  Widget _buildTextField(TextEditingController controller, String labelText,
      String? Function(String?) validator) {
    return TextFormField(
      controller: controller,
      decoration: InputDecoration(labelText: labelText),
      validator: validator,
    );
  }

  Widget _buildProtocolSelection() {
    return FormField<bool>(
      initialValue: controller.checkValue,
      builder: (formFieldState) {
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Text(
              "协议:",
              style: TextStyle(fontSize: 12),
            ),
            Row(
              children: [
                Checkbox(
                  value: !controller.checkValue,
                  onChanged: (value) {
                    formFieldState.didChange(value);
                    controller.checkValue = !value!;
                  },
                ),
                const Text("Http"),
                const SizedBox(width: 50),
                Checkbox(
                  value: controller.checkValue,
                  onChanged: (value) {
                    formFieldState.didChange(value);
                    controller.checkValue = value!;
                  },
                ),
                const Text("Https"),
              ],
            ),
          ],
        );
      },
    );
  }
}
